namespace PickGold.Charting.Design
{
    using System;
    using System.Globalization;
    using System.Windows.Forms;
    using System.Windows.Forms.Design;

    internal class AnchorPointNameTreeView : TreeView
    {
        private Annotation _annotation;
        private DataPoint _dataPoint;
        private IWindowsFormsEditorService _edSvc;

        public AnchorPointNameTreeView(IWindowsFormsEditorService edSvc, Annotation annotation, DataPoint dataPoint)
        {
            this._annotation = annotation;
            this._dataPoint = dataPoint;
            this._edSvc = edSvc;
            base.BorderStyle = BorderStyle.None;
            this.FillTree();
        }

        private void FillTree()
        {
            bool flag = false;
            base.BeginUpdate();
            TreeNode node = base.Nodes.Add("NotSet");
            if (((this._annotation != null) && (this._annotation.AnnotationGroup == null)) && (this._annotation.Chart != null))
            {
                foreach (Series series in this._annotation.Chart.Series)
                {
                    TreeNode node2 = base.Nodes.Add(series.Name);
                    node2.Tag = series;
                    if (series.Points.Count == 0)
                    {
                        node2.Nodes.Add("(empty)");
                    }
                    int num = 1;
                    foreach (DataPoint point in series.Points)
                    {
                        TreeNode node3 = node2.Nodes.Add("DataPoint" + num.ToString(CultureInfo.InvariantCulture));
                        node3.Tag = point;
                        num++;
                        if (point == this._dataPoint)
                        {
                            node2.Expand();
                            base.SelectedNode = node3;
                            flag = true;
                        }
                    }
                }
            }
            if (!flag)
            {
                base.SelectedNode = node;
            }
            base.EndUpdate();
        }

        public DataPoint GetNewValue()
        {
            if (((base.SelectedNode != null) && (base.SelectedNode.Tag != null)) && (base.SelectedNode.Tag is DataPoint))
            {
                return (DataPoint) base.SelectedNode.Tag;
            }
            return null;
        }

        protected override void OnDoubleClick(EventArgs e)
        {
            base.OnDoubleClick(e);
            if (this._edSvc != null)
            {
                if (this.GetNewValue() != null)
                {
                    this._edSvc.CloseDropDown();
                }
                else if ((base.SelectedNode != null) && (base.SelectedNode.Text == "NotSet"))
                {
                    this._edSvc.CloseDropDown();
                }
            }
        }
    }
}

